package com.breaker.kata.reverse;

import org.apache.commons.compress.utils.Lists;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Reverse {

    private static final String RUNG = "-";

    public static String reverse(String params) {
        if (!StringUtils.hasText(params)) {
            return params;
        }
        String[] strings = params.split("");
        List<Integer> indexOfs = filterAllIndexOf(params);
        String reverseString = new StringBuilder(Arrays.stream(strings).filter(string -> !RUNG.equals(string)).collect(Collectors.joining())).reverse().toString();
        for (Integer indexOf : indexOfs) {
            reverseString = reverseString.substring(0, indexOf) + "-" + reverseString.substring(indexOf);
        }
        return reverseString;
    }

    private static List<Integer> filterAllIndexOf(String params) {
        List<Integer> indexOfs = Lists.newArrayList();
        int index = params.indexOf("-");
        while (index != -1) {
            indexOfs.add(index);
            index = params.indexOf("-", index + 1);
        }
        return indexOfs;
    }
}
